package teste;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;

public class ModificadorTxt {
	
	private static String nomeArquivo;
	
	public static String leArquivo(String nomeArquivo) throws IOException {
		
		FileInputStream inputStream = new FileInputStream(nomeArquivo);
		BufferedInputStream buffer = new BufferedInputStream(inputStream);
		
        String saida = "";
        
        try {
        	int read = buffer.read();
			while(read != -1) {
				char taaa = (char) (read);
				saida += taaa;
				read = buffer.read();
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		
		} finally {
			if (inputStream != null)
					inputStream.close();
		 }
        
		return saida;
	}
	
	public static void geraSaida(String arquivoFormatado) {

		try {
			FileWriter fstream = new FileWriter(nomeArquivo);
			BufferedWriter out = new BufferedWriter(fstream);
			out.write(arquivoFormatado);
			out.close();
		} catch (Exception e) {
			System.err.println("Error: " + e.getMessage());
		}
	}
	
	public static String modificaLinhasDoArquivo(String nomeDoArquivo,
			int coluna, String mudanca) throws IOException {
		nomeArquivo = nomeDoArquivo;
		
		
		String arquivo = leArquivo(nomeArquivo);
		String[] splitString = arquivo.split("\n");
		String saida = "";
		
		String indicacaoSaida = " =";
		
		for (int i = 0; i < splitString.length; i++) {
			String[] splitLinha = splitString[i].split("");
			for (int j = 0; j < splitString[i].length(); j++) {
				if (j == coluna) {
					saida += mudanca;
				}
				saida += splitLinha[j];
			}
			saida += "\n";
			indicacaoSaida += "=";
		}
		
		indicacaoSaida += "> DONE";
		System.out.println(indicacaoSaida);
		return saida;
		
	}
	
	public static void main(String[] args) {
		try {
			String arquivoModificado = modificaLinhasDoArquivo("023003012011ElencoContas.txt", 11, "0000");
			geraSaida(arquivoModificado);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
